package com.canplay.screen.db

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase

@Database(entities = [DataItem::class], version = 2, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {
    abstract fun dataItemDao(): DataItemDao

    companion object {
        private const val dataBaseName = "salt-db"

        @Volatile
        private var instance: AppDatabase? = null
        fun getInstant(context: Context): AppDatabase {
            return instance ?: synchronized(this) {
                instance ?: buildDatabase(context).also { instance = it }
            }
        }

        private fun buildDatabase(context: Context): AppDatabase {
            return Room.databaseBuilder(context, AppDatabase::class.java, dataBaseName)
                .fallbackToDestructiveMigration().build()
        }
    }
}